<?php
/**
 * Created by PhpStorm.
 * User: Keter
 * Date: 2016/8/3
 * Time: 16:55
 */
class StatsLogBase extends DatabaseBase
{
    public $queenName;
    public $baseTableName;
    public $tableName;
    public $dbName;

    public function __construct()
    {
        parent::__construct();
        $this->initMysql();
    }

    /**
     * 重写isRedisLog()
     * @param bool $enabled
     */
    public function isRedisLog($enabled = false)
    {
        parent::isRedisLog($enabled); // TODO: Change the autogenerated stub
        $this->mysql->isRedisLog($enabled);
    }

    /**
     * 推入队列
     * @param $data
     * @param $error
     * @param $db
     * @return bool
     * @throws Exception
     */
    public function pushQueen($data, $error, $db = 'user_stats')
    {
        $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        $result = $this->redis($db)->lpush($this->queenName, $data);

        if (!$result) {
            return $this->throwError($error, -1);
        }

        return $result;
    }

    /**
     * 设置表名
     * @param $time
     * @param $format
     */
    public function setTableName($time = null, $format = 'Ym')
    {
        if ($time) {
            $this->tableName = $this->baseTableName . '_' . date($format, $time);
        } else {
            $this->tableName = $this->baseTableName;
        }
    }

    /**
     * 选择和检测数据库
     * @param $time
     * @param $format
     * @return bool
     */
    public function checkDatabase($time = null, $format = 'Y')
    {
        if ($this->dbName) {
            $dbName = $this->dbName;
        } else {
            $dbName = DatabaseConfig::$mysql_config['default']['dbName'];
        }

        if ($time) {
            $dbName = $dbName . '_' . date($format, $time);
        }

        $result = $this->mysql->selectDB($dbName);
        if (!$result) {
            $sql = "CREATE DATABASE IF NOT EXISTS {$dbName} DEFAULT CHARSET utf8 COLLATE utf8_general_ci;";
            $this->mysql->query($sql);
            $result = $this->mysql->selectDB($dbName);
            if (!$result) {
                $this->e($this->mysql->error());
            }
        }

        return $result;
    }

    /**
     * 检测表名是否存在
     * @param $tableName
     * @param $method
     * @return bool
     */
    public function checkTableExists($tableName = null, $method = 'getCreateTableSql')
    {
        $tableName = $tableName ? $tableName : $this->tableName;

        /**
         * 检查表是否存在
         */
        $existsTable = $this->mysql->tableExists($tableName);
        if (!$existsTable) {
            if (!method_exists($this, $method)) {
                $data['tableName'] = $tableName;
                $this->e('no_create_table_sql', $data);
                return false;
            }
            $ret = $this->mysql->query($this->$method($tableName));
            if (!$ret) {
                $data['tableName'] = $tableName;
                $this->e('create_table_mysql_error', $this->mysql->error());
                $this->e('create_table_error', $data);
                return false;
            }
        }

        return true;
    }
}